import logging

from colorlog import ColoredFormatter

# 创建logger对象
logger = logging.getLogger()

# 配置logger属性
logger.setLevel(logging.DEBUG)
# logger.setLevel(logging.INFO)

# formatter = logging.Formatter('%(asctime)s %(levelname)-2s %(message)s')

# 创建自定义格式化器
formatter = ColoredFormatter(
    "%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
    "cyan"
)

fileHandler = logging.FileHandler('./dy_logfile.log', encoding='utf-8')
fileHandler.setFormatter(formatter)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(formatter)

# 添加处理器到logger对象中
logger.addHandler(fileHandler)
logger.addHandler(consoleHandler)

if __name__ == '__main__':
    logger.info("这是一条info消息")
    logger.debug("这是一条debug消息")
    logger.error("这是一条error消息")
